package br.com.registrofinancas.adapters;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import br.com.registrofinancas.R;
import br.com.registrofinancas.model.ExpenseVO;
import br.com.registrofinancas.model.IncomeVO;
import br.com.registrofinancas.model.MovimentoOrigemVO;
import br.com.registrofinancas.util.CurrencyUtils;
import br.com.registrofinancas.view.CalendarioActivity;

public class FinancasGastosAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private final Context context;
	private List<ExpenseVO> expensesVO;
	
	public FinancasGastosAdapter(Context context, List<ExpenseVO> expenseVO) {
		this.context = context;
		this.expensesVO = expenseVO;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return expensesVO.size();
	}

	@Override
	public ExpenseVO getItem(int position) {
		return expensesVO.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolderItem viewHolderItem;

		ExpenseVO expenseVO = expensesVO.get(position);
		
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.activity_financas_item_gastos, parent, false);

			viewHolderItem = new ViewHolderItem();

			viewHolderItem.ivCheck = (ImageView) convertView.findViewById(R.id.activity_financas_item_gastos_iv_check);
			
			viewHolderItem.tvDescricao = (TextView) convertView.findViewById(R.id.activity_financas_item_gastos_tv_descricao);
			viewHolderItem.tvCategoria = (TextView) convertView.findViewById(R.id.activity_financas_item_gastos_tv_categoria);
			viewHolderItem.tvData = (TextView) convertView.findViewById(R.id.activity_financas_item_gastos_tv_data);
			viewHolderItem.tvValor = (TextView) convertView.findViewById(R.id.activity_financas_item_gastos_tv_valor);			
			viewHolderItem.tvOrigem = (TextView) convertView.findViewById(R.id.activity_financas_item_gastos_tv_origem);
			
			viewHolderItem.tvDescricao.setText(expenseVO.getDescription());
			viewHolderItem.tvCategoria.setText(expenseVO.getCategory().getName());
			viewHolderItem.tvData.setText(formater(expenseVO.getOperationDate()));
			viewHolderItem.tvValor.setText(CurrencyUtils.formataValor(expenseVO.getValue(), context));
			
			if (expenseVO.getMovimentosOrigem() != null) {			
				for (MovimentoOrigemVO movimentoOrigemVO : expenseVO.getMovimentosOrigem()) {
					viewHolderItem.tvOrigem.setText(movimentoOrigemVO.getSaida().getDescription());
				}				
			}
									
			convertView.setTag(viewHolderItem);

		} else {
			viewHolderItem = (ViewHolderItem) convertView.getTag();
		}

		GradientDrawable drawable = (GradientDrawable) context.getResources().getDrawable(R.drawable.financas_circle_verde);
		String cor = getRandomColor();
		drawable.setColor(Color.parseColor(cor));
		viewHolderItem.ivCheck.setBackground(drawable);
		
		return convertView;
	}
	
	public String formater(Date date){
		
		if (date == null) {
			return "";
		}
		
		SimpleDateFormat  formater = new SimpleDateFormat("EEE - dd/MM/yy");
		return formater.format(date);
	}


	private static class ViewHolderItem {
		TextView tvDescricao, tvCategoria, tvData, tvOrigem, tvValor;
		ImageView ivCheck;
	}

	public static String getRandomColor() {
		String[] letters = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
		String color = "#";
		for (int i = 0; i < 6; i++) {
			color += letters[(int) Math.round(Math.random() * 15)];
		}
		return color;
	}

}
